{% extends "base.html" %}

{% block title %}论坛 - SteHub{% endblock %}

{% block content %}
<div class="row">
    <!-- 左侧内容 -->
    <div class="col-lg-9">
        <!-- 论坛头部 -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2><i class="fas fa-comments me-2"></i>论坛</h2>
            {% if current_user.is_authenticated %}
            <a href="{{ url_for('forum.create_post') }}" class="btn btn-primary">
                <i class="fas fa-plus me-2"></i>发布新帖
            </a>
            {% endif %}
        </div>

        <!-- 搜索和筛选 -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET" action="{{ url_for('forum.posts') }}" class="row g-3">
                    <div class="col-md-4">
                        <select name="category" class="form-select">
                            <option value="">全部分类</option>
                            {% for category in categories %}
                            <option value="{{ category.id }}" {% if current_category == category.id %}selected{% endif %}>
                                {{ category.name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <select name="sort" class="form-select">
                            <option value="newest" {% if sort_by == 'newest' %}selected{% endif %}>最新发布</option>
                            <option value="popular" {% if sort_by == 'popular' %}selected{% endif %}>最多浏览</option>
                            <option value="hot" {% if sort_by == 'hot' %}selected{% endif %}>热门帖子</option>
                        </select>
                    </div>
                    <div class="col-md-4">
                        <input type="text" name="q" class="form-control" placeholder="搜索帖子..." value="{{ search_query }}">
                    </div>
                    <div class="col-md-1">
                        <button type="submit" class="btn btn-outline-primary w-100">
                            <i class="fas fa-search"></i>
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <!-- 帖子列表 -->
        <div class="card">
            <div class="card-header bg-light">
                <div class="row align-items-center">
                    <div class="col-md-6">
                        <strong>帖子</strong>
                    </div>
                    <div class="col-md-2 text-center">
                        <strong>作者</strong>
                    </div>
                    <div class="col-md-1 text-center">
                        <strong>回复</strong>
                    </div>
                    <div class="col-md-1 text-center">
                        <strong>浏览</strong>
                    </div>
                    <div class="col-md-2 text-center">
                        <strong>最后更新</strong>
                    </div>
                </div>
            </div>
            <div class="list-group list-group-flush">
                {% if posts %}
                    {% for post in posts %}
                    <div class="list-group-item {% if post.is_sticky %}bg-warning bg-opacity-10{% endif %}">
                        <div class="row align-items-center">
                            <div class="col-md-6">
                                <div class="d-flex align-items-start">
                                    <div class="flex-shrink-0 me-3">
                                        {% if post.is_sticky %}
                                        <i class="fas fa-thumbtack text-warning" title="置顶帖子"></i>
                                        {% elif post.is_featured %}
                                        <i class="fas fa-star text-danger" title="精华帖子"></i>
                                        {% else %}
                                        <i class="fas fa-file-alt text-muted"></i>
                                        {% endif %}
                                    </div>
                                    <div class="flex-grow-1">
                                        <h6 class="mb-1">
                                            <a href="{{ url_for('forum.view_post', post_id=post.id) }}" class="text-decoration-none">
                                                {{ post.title }}
                                            </a>
                                        </h6>
                                        <div class="small text-muted">
                                            <span class="badge" style="background-color: {{ post.category.color }};">
                                                {{ post.category.name }}
                                            </span>
                                            {% if post.is_sticky %}
                                            <span class="badge bg-warning ms-1">置顶</span>
                                            {% endif %}
                                            {% if post.is_featured %}
                                            <span class="badge bg-danger ms-1">精华</span>
                                            {% endif %}
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-2 text-center">
                                <div class="small">
                                    <a href="#" class="text-muted text-decoration-none">{{ post.author.username }}</a>
                                </div>
                            </div>
                            <div class="col-md-1 text-center">
                                <span class="badge bg-light text-dark">{{ post.comment_count }}</span>
                            </div>
                            <div class="col-md-1 text-center">
                                <span class="badge bg-light text-dark">{{ post.view_count }}</span>
                            </div>
                            <div class="col-md-2 text-center">
                                <small class="text-muted">{{ post.updated_at|relative_time }}</small>
                            </div>
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <div class="list-group-item text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <h5 class="text-muted">暂无帖子</h5>
                        <p class="text-muted">还没有帖子，快来发布第一个帖子吧！</p>
                        {% if current_user.is_authenticated %}
                        <a href="{{ url_for('forum.create_post') }}" class="btn btn-primary">
                            <i class="fas fa-plus me-2"></i>发布新帖
                        </a>
                        {% else %}
                        <a href="{{ url_for('auth.login') }}" class="btn btn-primary">
                            <i class="fas fa-sign-in-alt me-2"></i>登录后发帖
                        </a>
                        {% endif %}
                    </div>
                {% endif %}
            </div>
        </div>

        <!-- 分页 -->
        {% if pagination.pages > 1 %}
        <nav aria-label="帖子分页" class="mt-4">
            <ul class="pagination justify-content-center">
                {% if pagination.has_prev %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('forum.posts', page=pagination.prev_num, category=current_category, sort=sort_by, q=search_query) }}">
                        上一页
                    </a>
                </li>
                {% endif %}

                {% for page_num in pagination.iter_pages() %}
                    {% if page_num %}
                        <li class="page-item {% if page_num == pagination.page %}active{% endif %}">
                            <a class="page-link" href="{{ url_for('forum.posts', page=page_num, category=current_category, sort=sort_by, q=search_query) }}">
                                {{ page_num }}
                            </a>
                        </li>
                    {% else %}
                        <li class="page-item disabled">
                            <span class="page-link">...</span>
                        </li>
                    {% endif %}
                {% endfor %}

                {% if pagination.has_next %}
                <li class="page-item">
                    <a class="page-link" href="{{ url_for('forum.posts', page=pagination.next_num, category=current_category, sort=sort_by, q=search_query) }}">
                        下一页
                    </a>
                </li>
                {% endif %}
            </ul>
        </nav>
        {% endif %}
    </div>

    <!-- 右侧边栏 -->
    <div class="col-lg-3">
        <!-- 分类列表 -->
        <div class="card mb-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="fas fa-tags me-2"></i>全部分类</h6>
            </div>
            <div class="list-group list-group-flush">
                <a href="{{ url_for('forum.posts') }}" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center {% if not current_category %}active{% endif %}">
                    全部帖子
                    <span class="badge bg-primary rounded-pill">{{ categories|sum(attribute='post_count') }}</span>
                </a>
                {% for category in categories %}
                <a href="{{ url_for('forum.posts', category=category.id) }}" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center {% if current_category == category.id %}active{% endif %}">
                    {{ category.name }}
                    <span class="badge rounded-pill" style="background-color: {{ category.color }};">{{ category.post_count }}</span>
                </a>
                {% endfor %}
            </div>
        </div>

        <!-- 论坛统计 -->
        <div class="card">
            <div class="card-header">
                <h6 class="mb-0"><i class="fas fa-chart-bar me-2"></i>论坛统计</h6>
            </div>
            <div class="card-body">
                <div class="row text-center">
                    <div class="col-6">
                        <h5 class="text-primary">{{ pagination.total if pagination else 0 }}</h5>
                        <small class="text-muted">总帖子</small>
                    </div>
                    <div class="col-6">
                        <h5 class="text-success">{{ categories|length }}</h5>
                        <small class="text-muted">分类数</small>
                    </div>
                </div>
            </div>
        </div>

        <!-- 发帖指南 -->
        <div class="card mt-4">
            <div class="card-header">
                <h6 class="mb-0"><i class="fas fa-lightbulb me-2"></i>发帖指南</h6>
            </div>
            <div class="card-body">
                <ul class="small text-muted mb-0">
                    <li>选择正确的分类</li>
                    <li>标题清晰明了</li>
                    <li>内容详细具体</li>
                    <li>遵守社区规则</li>
                </ul>
            </div>
        </div>
    </div>
</div>
{% endblock %}